package com.lpy.book.JFrame;

import com.lpy.book.model.BorrowDetail;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserBorrowDetail extends JFrame {
    private List<BorrowDetail> borrowDetails;  // 模拟已借书籍信息列表
    private DefaultTableModel borrowTableModel;
    private JTable borrowTable; // 添加 borrowTable 变量

    public UserBorrowDetail() {
        // 初始化 borrowDetails（模拟数据）
        borrowDetails = new ArrayList<>();
        borrowDetails.add(new BorrowDetail(1, 1, 101, 1, System.currentTimeMillis(), null));
        borrowDetails.add(new BorrowDetail(2, 1, 102, 1, System.currentTimeMillis(), null));

        // 设置窗口属性
        setTitle("图书管理系统");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 创建还书表格模型
        String[] columnNames = {"借阅ID", "书籍ID", "借书时间", "还书时间"};
        borrowTableModel = new DefaultTableModel(columnNames, 0);
        updateBorrowTable();

        // 创建还书表格
        borrowTable = new JTable(borrowTableModel);

        // 创建还书按钮
        JButton returnButton = new JButton("还书");
        returnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showReturnDialog();
            }
        });

        // 创建退出系统按钮
        JButton exitButton = new JButton("退出系统");
        exitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        // 创建按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout());
        buttonPanel.add(returnButton);
        buttonPanel.add(exitButton);

        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.add(new JScrollPane(borrowTable), BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        // 将主面板添加到窗口中
        add(mainPanel);

        // 将窗口居中
        setLocationRelativeTo(null);

        // 显示窗口
        setVisible(true);
    }

    // 更新还书表格模型。
    private void updateBorrowTable() {
        borrowTableModel.setRowCount(0);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (BorrowDetail detail : borrowDetails) {
            Object[] rowData = {
                    detail.getBorrowId(),
                    detail.getBookId(),
                    dateFormat.format(new Date(detail.getBorrowTime())),
                    detail.getReturnTime() != null ? dateFormat.format(new Date(detail.getReturnTime())) : ""
            };
            borrowTableModel.addRow(rowData);
        }
    }

    // 显示还书对话框
    private void showReturnDialog() {
        JFrame returnDialog = new JFrame("还书");
        returnDialog.setSize(300, 200);

        // 创建还书按钮
        JButton confirmReturnButton = new JButton("确认还书");
        confirmReturnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int selectedRow = borrowTable.getSelectedRow();
                if (selectedRow != -1) {
                    // 处理还书逻辑，可以更新数据库等
                    BorrowDetail selectedBorrow = borrowDetails.get(selectedRow);
                    if (selectedBorrow.getReturnTime() == null) {
                        selectedBorrow.setStatus(2);  // 设置状态为已还
                        selectedBorrow.setReturnTime(System.currentTimeMillis());  // 设置还书时间
                        updateBorrowTable();  // 更新还书表格
                        JOptionPane.showMessageDialog(returnDialog, "还书成功！");
                        returnDialog.dispose();  // 关闭还书对话框
                    } else {
                        JOptionPane.showMessageDialog(returnDialog, "该书已还过！");
                    }
                } else {
                    JOptionPane.showMessageDialog(returnDialog, "请选择要还的书籍！");
                }
            }
        });

        // 将组件添加到还书对话框中
        returnDialog.add(confirmReturnButton);

        // 设置还书对话框属性
        returnDialog.setLayout(new FlowLayout());
        returnDialog.setLocationRelativeTo(this);
        returnDialog.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new UserBorrowDetail();
            }
        });
    }
}





